﻿using System.Globalization;
using System.Xml;

namespace Smartstore
{
    public static class XmlNodeExtensions
    {
        /// <summary>
        /// Safe way to get inner text of an attribute.
        /// </summary>
        public static T GetAttributeText<T>(this XmlNode node, string attributeName, T defaultValue = default)
        {
            try
            {
                if (node != null && attributeName.HasValue())
                {
                    XmlAttribute attr = node.Attributes[attributeName];
                    if (attr != null)
                    {
                        return attr.InnerText.Convert<T>();
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Dump();
            }

            return defaultValue;
        }

        /// <summary>
        /// Safe way to get inner text of an attribute.
        /// </summary>
        public static string GetAttributeText(this XmlNode node, string attributeName)
        {
            return node.GetAttributeText<string>(attributeName, null);
        }

        /// <summary>
        /// Safe way to get inner text of a node.
        /// </summary>
        public static T GetText<T>(this XmlNode node, string xpath = null, T defaultValue = default, CultureInfo culture = null)
        {
            try
            {
                if (node != null)
                {
                    if (xpath.IsEmpty())
                    {
                        return node.InnerText.Convert<T>();
                    }

                    var n = node.SelectSingleNode(xpath);

                    if (n != null && n.InnerText.HasValue())
                    {
                        return n.InnerText.Convert<T>(culture);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Dump();
            }

            return defaultValue;
        }

        /// <summary>
        /// Safe way to get inner text of a node.
        /// </summary>
        public static string GetText(this XmlNode node, string xpath = null, string defaultValue = default)
        {
            return node.GetText<string>(xpath, defaultValue);
        }
    }
}
